clear, close all
NumOfStocks = 20;
NumOfBlocks = 7;

%All data loaded to retain fixed values for comparison purposes

%[CovBlocks, Combinations, SampleSizes, Sigma, StockReturns]=GetCovBlocks(0);
load data_7_blocks.mat

% norms = {'fro', 1, 2};
noise = 10*[4 1 2 3 1 3 2];
Samples=50*ones(1,NumOfBlocks);


%CovBlocksSamples = GetCovBlocksSamples(noise, CovBlocks, Samples);
load Noisy_data_7_blocks_50_samples.mat

    cvx_begin
%        cvx_quiet(true); 
        variable Sigma_hat(NumOfStocks, NumOfStocks) symmetric;
    
        %Define objective function
        f = 0;
        h=cell(NumOfBlocks);
        s_max = 0;
        for q=1:NumOfBlocks
            temp=[];
            for j=1:Samples(1,q)
                %Add the norm of differences between block covariance and the
                %corresponding block in X.

                temp=[temp,(1/(Samples(1,q)))*norm(Sigma_hat(Combinations{q}, Combinations{q})-CovBlocksSamples{q}{j},'fro')];
                
            end
            h{q}=temp;
        end
        
        for i=1:NumOfBlocks
            f = f + max(h{q});
        end
        
        minimize (f)
    
        subject to
        
        %Generate sample average to use in constraints
        load Noisy_data_7_blocks_50_samples_averages.mat
%                 CovBlocksSamplesAverage=cell(NumOfBlocks,1);
         for p=1:NumOfBlocks

%              CovBlocksSamplesAverage{p,1}=zeros(size(CovBlocksSamples{p}{1}));
%              for r=1:Samples(1,p)
%              CovBlocksSamplesAverage{p,1}=CovBlocksSamplesAverage{p,1} + (1/Samples(1,p))*CovBlocksSamples{p}{r};
%              end
        %Add constraint on each element of the block so it doesn't esceed 2times the sampled average of that value for each block.
              abs(Sigma_hat(Combinations{p}, Combinations{p}) - CovBlocksSamplesAverage{p,1}) < abs(CovBlocksSamplesAverage{p,1})
                          
         end

        
        Sigma_hat == semidefinite(NumOfStocks)
     cvx_end
     